#ifndef Mailer_h
#define Mailer_h

#include "Buffer.h"
#include "BoundedBuffer.h"
#include "Thread.h"
#include "System.h"
#include "Message.h"
#include <map>

using std::map;

typedef struct MAILBOX
{
	int id;
	Buffer* mailbox;
	int maxLength;
} MAILBOX;

typedef map<int, MAILBOX> MAILBOXES;

class Mailer : public iSchedulable
{
public:
	Mailer(System* sys, int inboxSize);
	~Mailer(void);

	int send(int dest, Message* msg);
	Message* recieve(int id);
	void addMailbox(int id);
	int getMailboxSize(int id);
	void stop();
	bool isBusy();
	void startCounting();
	void stopCounting();
	int getMaxLength();
	int getInboxMaxLength(int id);
	int getTimeRequest();
private:
	System* m_system;
	BoundedBuffer* m_inbox;
	Thread* m_dispatcher;
	MAILBOXES* m_mailboxes;
	bool m_shouldRun;
	bool m_busy;
	int m_maxQueue;
	bool m_counting;


	static void* dispatch(void* arg);
};

#endif
